package com.cby.android.library.utils

import android.app.Activity
import com.cby.android.olive.ext.showToast
import com.google.zxing.client.android.MNScanManager
import com.google.zxing.client.android.model.MNScanConfig

/**
 * @author : maning
 * @date : 2020-10-16
 * @desc :
 */
object ZxingScanUtils {
    private const val COLOR_MAIN = "#62e203"
    fun open(activity: Activity?) {
        //自定义扫描
        val scanConfig: MNScanConfig = MNScanConfig.Builder() //设置完成震动
            .isShowVibrate(false) //扫描完成声音
            .isShowBeep(true) //显示相册功能
            .isShowPhotoAlbum(true) //显示闪光灯
            .isShowLightController(true) //自定义文案
            .setScanHintText("请对准二维码/条形码识别") //扫描线的颜色
            .setScanColor(COLOR_MAIN) //是否显示缩放控制器
            .isShowZoomController(false) //扫描线样式
            .setLaserStyle(MNScanConfig.LaserStyle.Line) //是否全屏范围扫描
            .setFullScreenScan(true) //二维码标记点
            .isShowResultPoint(true)
            .setResultPointConfigs(60, 30, 10, "#FFFFFFFF", "#7000A81F")
            .builder()
        MNScanManager.startScan(activity, scanConfig
        ) { resultCode, data ->
            when (resultCode) {
                MNScanManager.RESULT_SUCCESS -> {
                    val resultSuccess =
                        data.getStringExtra(MNScanManager.INTENT_KEY_RESULT_SUCCESS)

                    activity!!.showToast("扫描成功")
                    //跳转到扫码结果页面
                    //ScanResultActivity.open(activity, resultSuccess)
                }
                MNScanManager.RESULT_FAIL -> {
                    val resultError =
                        data.getStringExtra(MNScanManager.INTENT_KEY_RESULT_ERROR)
                    if (resultError != null) {
                        activity!!.showToast(resultError)
                    }
                }
                MNScanManager.RESULT_CANCLE -> {
                }
            }
        }
    }
}